package com.chhuang.ewcrm.thread;

import com.chhuang.ewcrm.App;
import com.chhuang.ewcrm.bean.TableColumnBean;
import com.chhuang.ewcrm.cons.Constants;
import com.chhuang.ewcrm.dao.WWeighTempDao;
import com.chhuang.ewcrm.date.DateUtil;
import com.chhuang.ewcrm.entity.WCustomer;
import com.chhuang.ewcrm.entity.WWeighTemp;
import javafx.beans.binding.DoubleBinding;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static javafx.application.Platform.runLater;

/**
 * 初始化表格
 * @author chhuang
 * @create 2020-10-12-14:50
 */
public class InitTableThread implements Runnable{
    private final Logger logger = LoggerFactory.getLogger(InitTableThread.class);

    private final TableView<WWeighTemp> weighListTableView;
    private final Label numberLabel;

    private final WWeighTempDao wWeighTempDao = App.getSession().getMapper(WWeighTempDao.class);

    private TableColumnBean[] tableColumnBeans = {
            new TableColumnBean("过磅类别","weighType", true){
                @Override
                public String format(Object o) {
                    if(o==null) return null;
                    return Constants.WEIGH_TYPES.get((int)o).getWeighTypeName();
                }
            },
            new TableColumnBean("车牌号","licensePlate"),
            new TableColumnBean("仓库","warehouseName", true),
            new TableColumnBean("客户","customerName"),
            new TableColumnBean("物品","goodsName", true),
            new TableColumnBean("原仓库","warehouse1Name"),
            new TableColumnBean("物品原名","goods1Name"),
            new TableColumnBean("毛重","grossWeight"),
            new TableColumnBean("皮重","tareWeight"),
            new TableColumnBean("扣重","impurityWeight"),
            new TableColumnBean("净重","netWeight"),
            new TableColumnBean("净重","netWeight"),
            new TableColumnBean("价格","price"),
            new TableColumnBean("金额","amount"),
            new TableColumnBean("过磅员","weighmanName", true),
            new TableColumnBean("备注","remark"),
            new TableColumnBean("负责人","chargemanName", true),
            new TableColumnBean("业务员","salesmanName", true),
            new TableColumnBean("进厂时间","enterTime", true){
                @Override
                public String format(Object o) {
                    if(o==null) return null;
                    return DateUtil.date2String((Date)o, DateUtil.YYYY_MM_DD_HH_MM_SS);
                }
            }
    };

    public InitTableThread(TableView<WWeighTemp> weighListTableView, Label numberLabel){
        this.weighListTableView = weighListTableView;
        this.numberLabel = numberLabel;
    }

    private List<TableColumn<WWeighTemp, Object>> getTableColumns() {
        //设置列
        List<TableColumn<WWeighTemp, Object>> tableColumns = new LinkedList<>();
        for (TableColumnBean tcb : tableColumnBeans){
            TableColumn<WWeighTemp, Object> tableColumn = new TableColumn<>(tcb.getColumnName());
            tableColumn.setCellValueFactory(new PropertyValueFactory<>(tcb.getRecordName()));
            tableColumn.setSortable(tcb.isSort());
            tableColumn.setVisible(tcb.isVisible());
            tableColumn.setCellFactory(wWeighTempObjectTableColumn -> {
                TableCell<WWeighTemp, Object> cell = new TableCell<>(){
                    @Override
                    protected void updateItem(Object o, boolean b) {
                        super.updateItem(o, b);
                        if(b || o==null){
                            this.setText(null);
                        }else {
                            this.setText(tcb.format(o));
                        }
                    }
                };
                cell.setAlignment(Pos.CENTER);
                return cell;
            });
            tableColumns.add(tableColumn);
        }

        return tableColumns;
    }


    @Override
    public void run() {
        final List<TableColumn<WWeighTemp, Object>> tableColumns = getTableColumns();
        WWeighTemp wWeighTemp = new WWeighTemp();
        wWeighTemp.setStatus(-1);//-1表示需要二次过磅的数据
        final List<WWeighTemp> wWeighTemps = wWeighTempDao.query(wWeighTemp);
        runLater(()->{
            weighListTableView.getColumns().addAll(tableColumns);//添加列
            if(wWeighTemps!=null && !wWeighTemps.isEmpty()) {
                weighListTableView.getItems().addAll(wWeighTemps);//添加数据
                numberLabel.setText(String.valueOf(wWeighTemps.size()));
            }else {
                numberLabel.setText("0");
            }
        });
    }

}
